//
//  HXSelecteManageViewController.m
//  KD
//
//  Created by ifly on 16/5/11.
//  Copyright © 2016年 Whx. All rights reserved.
//
#define DeptViewTag 10
#define GroupViewTag 11

#define DeptStr @"部门"
#define GroupStr @"小组"

#import "HXSelecteManageViewController.h"
#import "HXBtnSelectedView.h"
#import "HXSelectedPersonResultView.h"

@interface HXSelecteManageViewController ()<UIGestureRecognizerDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate,HXBtnSelectedViewDelegate,HXSelectedManagePersonListModel,HXSelectedPersonResultViewDelegate>
@property(strong,nonatomic)UIView *maskView;
@property(nonatomic,strong) UIToolbar* keyboardDoneButtonBar;
@property(nonatomic,strong) UITapGestureRecognizer *tapGestureRec;
@property(strong,nonatomic)HXSelectedPersonResultView *selectedPerson;//人员展示view
/**网络数据-具体人员*/
@property(nonatomic,strong) NSArray<HXSelectedManagePersonListModel *> *personArray;
//@property(nonatomic,strong) NSArray<HXManageGroupListModel *> *groupArray;
@property(nonatomic,strong) NSArray<HXManageDepartmentListModel *> *deptArray;
@property (weak, nonatomic) IBOutlet UITextField *selecteTextFeild;
@property (weak, nonatomic) IBOutlet UIView *deptSelectedView;
//@property (weak, nonatomic) IBOutlet UIView *groupKindSelectedView;
@property(nonatomic,strong) HXBtnSelectedView *deptBtnSelectedView;
//@property(nonatomic,strong) HXBtnSelectedView *groupBtnSelectedView;
@property (strong, nonatomic)  UITextField *deptTextField;//tag DeptViewTag
//@property (strong, nonatomic)  UITextField *groupTextField;//tag GroupViewTag
/**地区的pickerView*/
@property(nonatomic,strong) UIPickerView *deptPickerView;//tag DeptViewTag
/**企业类型的pickerView*/
//@property(nonatomic,strong) UIPickerView *groupPickerView;//tag GroupViewTag
@end

@implementation HXSelecteManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.maskView.alpha = 0.2;
    [self setUpBtnSelectedView];
    [NetworkAPI getManageDepartmentSuccess:^(HXManageDepartmentModel *result) {
        if ([result.code isEqualToString:@"200"]) {
            self.deptArray = result.resultlist;
        }else{
            [SVProgressHUD showErrorWithStatus:@"拉取主管部门错误"];
        }
    } failure:^(NSError *err) {
        [SVProgressHUD showErrorWithStatus:@"拉取主管部门错误"];
    }];
}

-(void)setUpBtnSelectedView{
    HXBtnSelectedView *areaBtnSelectedView = [HXBtnSelectedView initWithName:DeptStr];
    areaBtnSelectedView.frame = CGRectMake(0, 0, 285, 40);
    areaBtnSelectedView.delegate = self;
    [self.deptSelectedView addSubview:areaBtnSelectedView];
    self.deptBtnSelectedView = areaBtnSelectedView;
}
//只搜索文字
- (IBAction)searchBtnClick:(id)sender {
    if ([GlobalUtil isNilValue:self.selecteTextFeild.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确企业名称"];
        return;
    }
    [self.view endEditing:YES];
    NSMutableDictionary *mDict= [[NSMutableDictionary alloc]init];
    [mDict setObject:self.selecteTextFeild.text forKey:@"PersonName"];
    [self refreshData:mDict];
}
//条件搜索
- (IBAction)searchTwoBtnclick:(id)sender {
    NSMutableDictionary *mDict = [[NSMutableDictionary alloc]init];
    NSString *deptStr = [self.deptBtnSelectedView allCode];
    [mDict setObject:deptStr forKey:@"DeptID"];
//    NSString *groupStr = [self.groupBtnSelectedView allCode];
    [mDict setObject:@"" forKey:@"GroupID"];
    if ([GlobalUtil isNilValue:self.selecteTextFeild.text]) {
        [mDict setObject:@"" forKey:@"PersonName"];
    }else{
        [mDict setObject:self.selecteTextFeild.text forKey:@"PersonName"];
    }
//    [mDict setObject:self.selecteTextFeild.text forKey:@"PersonName"];
    [self refreshData:mDict];
}


- (void)selectedPersonOkBtnClick {
    if ([self.delegate respondsToSelector:@selector(getManagePersonMsg:nameArray:)]) {
        [self.delegate getManagePersonMsg:self.selectedPerson.personIDArray nameArray:self.selectedPerson.personNameArray];
        [self.selectedPerson removeFromSuperview];
        [self.selectedPerson.personIDArray removeAllObjects];
        [self.selectedPerson.personNameArray removeAllObjects];
        [self.maskView removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//搜索人员
-(void)refreshData:(NSMutableDictionary *)mDict{
    [SVProgressHUD showWithStatus:@"正在获取人员" ];
    [NetworkAPI getManagePersonWithNum:@"0"
                                  dict:mDict
                               success:^(HXSelectedManagePersonModel *result) {
                                   [SVProgressHUD dismiss];
                                   if ([result.code isEqualToString:@"200"]) {
                                       [self.view addSubview:self.maskView];
                                       [self.view addSubview:self.selectedPerson];
                                       self.personArray = result.masterlist;
                                       self.selectedPerson.data = self.personArray;
                                   }else{
                                       UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"获取失败" message:@"没有符合该条件的人员" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                                       [alert show];
                                   }
                               } failure:^(NSError *err) {
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                               }];
}

#pragma mark - UIPickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag == DeptViewTag){
        return self.deptArray.count;
    }else{
        return 0;
    }
    return 0;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == DeptViewTag) {
        HXManageDepartmentListModel *model = self.deptArray[row];
        return model.Dept_Name;
    }
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView.tag == DeptViewTag) {
        HXManageDepartmentListModel *model = self.deptArray[row];
        self.deptTextField.text = [NSString stringWithFormat:@"%@*%@",model.Dept_Name,model.ID];
    }
}

#pragma mark - TextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    HXLog(@"%@",textField.text);
    if ([GlobalUtil isNilValue:textField.text]) return;
    if ([textField.text isEqualToString:@"*"]) return;


    if (textField.tag == DeptViewTag) {
        [self.deptBtnSelectedView giveMeMsg:textField.text];
        HXLog(@"%@",textField.text);
    }
}

#pragma mark - HXBtnSelectedViewDelegate
- (void)addMsgWithName:(NSString*)name{
    if ([name isEqualToString:DeptStr]) {
        [self.deptTextField becomeFirstResponder];
    }
}


#pragma mark - 懒加载
- (UITextField *)deptTextField{
    if (_deptTextField == nil) {
        _deptTextField = [[UITextField alloc]init];
        _deptTextField.tag = DeptViewTag;
        _deptTextField.delegate = self;
        _deptTextField.inputView = self.deptPickerView;
        [self.deptSelectedView addSubview:_deptTextField];
        _deptTextField.inputAccessoryView = self.keyboardDoneButtonBar;
    }
    return _deptTextField;
}



-(UIToolbar*)keyboardDoneButtonBar{
    if(!_keyboardDoneButtonBar){
        _keyboardDoneButtonBar= [[UIToolbar alloc] init];
        _keyboardDoneButtonBar.barStyle = UIBarStyleDefault;
        _keyboardDoneButtonBar.tintColor = nil;
        [_keyboardDoneButtonBar sizeToFit];
        UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                  target:nil
                                                                                  action:nil];
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn addTarget:self action:@selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
        cancelBtn.size = CGSizeMake(44, 44);
        [cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
        
        UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [okBtn addTarget:self action:@selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
        okBtn.size = CGSizeMake(44, 44);
        [okBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [okBtn setTitle:@"确定" forState:UIControlStateNormal];
        okBtn.titleLabel.textColor = [UIColor blueColor];
        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
        [_keyboardDoneButtonBar setItems:[NSArray arrayWithObjects:cancelButton,flexItem,doneButton, nil]];
        
    }
    return _keyboardDoneButtonBar;
}
-(void)CancelClicked:(UIButton *)btn{
    self.deptTextField.text = @"";
    [self.view endEditing:YES];
}

-(void)DoneClicked:(UIButton *)btn{
    
    [self.view endEditing:YES];
}

- (UITapGestureRecognizer *)tapGestureRec{
    if (_tapGestureRec == nil) {
        _tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeSideBar)];
        _tapGestureRec.delegate=self;
    }
    return _tapGestureRec;
}

-(void)closeSideBar{
    [self.selectedPerson removeFromSuperview];
    [self.selectedPerson.personIDArray removeAllObjects];
    [self.selectedPerson.personNameArray removeAllObjects];
    [self.maskView removeFromSuperview];
}

- (UIView *)maskView{
    
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.userInteractionEnabled = YES;
        [_maskView addGestureRecognizer:self.tapGestureRec];
    }
    return _maskView;
}

- (HXSelectedPersonResultView *)selectedPerson{
    if (_selectedPerson == nil) {
        _selectedPerson = [HXSelectedPersonResultView viewWithXIB:nil];//280*433
        _selectedPerson.layer.cornerRadius = 7;
        _selectedPerson.cellType = @"zhuguan";
        _selectedPerson.delegate = self;
        //        [self.view addSubview:self.maskView];
        _selectedPerson.x = (HXSCREEN_WIDTH - 280)*0.5;
        _selectedPerson.y = 80;
        //        [self.view addSubview:_selectedPerson];
    }
    return _selectedPerson;
}

- (UIPickerView *)deptPickerView{
    if (_deptPickerView == nil) {
        _deptPickerView = [[UIPickerView alloc]init];
        _deptPickerView.dataSource = self;
        _deptPickerView.delegate = self;
        _deptPickerView.tag = DeptViewTag;
    }
    return _deptPickerView;
}

@end
